﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;

public partial class Main : System.Web.UI.MasterPage
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;    

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();

        if (!IsPostBack)
        {
            FillNews();
            FillArticles();

            FillMasters();
            FillCourses();
            FillActs();
            FillReports();
            FillPartners();
            FillContributions();
        }
    }

    private void FillNews()
    {
        IList<NodeVO> list = m_PostService.GetNodeListByParentId(3);
        if (list != null && list.Count > 0)
        {
            hlinkNews.NavigateUrl = string.Format("{0}?node={1}", "news.aspx", list[0].NodeId);
            RepeaterNews.DataSource = list.Take(4);
            RepeaterNews.DataBind();
        }        
    }

    private void FillArticles()
    {        
        IList<NodeVO> list = m_PostService.GetNodeListByParentId(12);
        if (list != null && list.Count > 0)
        {
            hlinkArticles.NavigateUrl = string.Format("{0}?node={1}", "article.aspx", list[0].NodeId);
            RepeaterArticles.DataSource = list.Take(4);
            RepeaterArticles.DataBind();
        }        
    }

    private void FillMasters()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();        
        conditions.Add("NodeId", "7");
        conditions.Add("Flag", "1");
        conditions.Add("PageIndex", "0");
        conditions.Add("PageSize", "4");
        IList<PostVO> list = m_PostService.GetPostList(conditions);
        if (list != null && list.Count > 0)
        {
            hlinkMasters.NavigateUrl = string.Format("{0}?id={1}", "master.aspx", list[0].PostId);
        }
        RepeaterMasters.DataSource = list;
        RepeaterMasters.DataBind();
    }

    private void FillCourses()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", "8");
        conditions.Add("Flag", "1");
        conditions.Add("PageIndex", "0");
        conditions.Add("PageSize", "3");
        IList<PostVO> list = m_PostService.GetPostList(conditions);
        if (list != null && list.Count > 0)
        {
            hlinkCourses.NavigateUrl = string.Format("{0}?id={1}", "course.aspx", list[0].PostId);
        }
        RepeaterCourses.DataSource = list;
        RepeaterCourses.DataBind();
    }

    private void FillActs()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", "9");
        conditions.Add("Flag", "1");
        conditions.Add("PageIndex", "0");
        conditions.Add("PageSize", "1");
        IList<PostVO> list = m_PostService.GetPostList(conditions);
        if (list != null && list.Count > 0)
        {
            hlinkActs.NavigateUrl = string.Format("{0}?id={1}", "act.aspx", list[0].PostId);
        }
    }

    private void FillReports()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", "10");
        conditions.Add("Flag", "1");
        conditions.Add("PageIndex", "0");
        conditions.Add("PageSize", "4");
        IList<PostVO> list = m_PostService.GetPostList(conditions);
        if (list != null && list.Count > 0)
        {
            hlinkReports.NavigateUrl = string.Format("{0}?id={1}", "report.aspx", list[0].PostId);
            hlinkReports2.NavigateUrl = string.Format("{0}?id={1}", "report.aspx", list[0].PostId);
        }
        RepeaterReports.DataSource = list;
        RepeaterReports.DataBind();
    }

    private void FillContributions()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", "15");
        conditions.Add("Flag", "1");
        conditions.Add("PageIndex", "0");
        conditions.Add("PageSize", "4");
        IList<PostVO> list = m_PostService.GetPostList(conditions);
        if (list != null && list.Count > 0)
        {
            hlinkContributions.NavigateUrl = string.Format("{0}?id={1}", "contribution.aspx", list[0].PostId);
        }
        RepeaterContributions.DataSource = list;
        RepeaterContributions.DataBind();
    }

    private void FillPartners()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", "11");
        conditions.Add("Flag", "1");
        conditions.Add("PageIndex", "0");
        conditions.Add("PageSize", "4");
        IList<PostVO> list = m_PostService.GetPostList(conditions);
        if (list != null && list.Count > 0)
        {
            hlinkPartners.NavigateUrl = string.Format("{0}?id={1}", "partner.aspx", list[0].PostId);
            hlinkPartners2.NavigateUrl = string.Format("{0}?id={1}", "partner.aspx", list[0].PostId);
        }
        RepeaterPartners.DataSource = list;
        RepeaterPartners.DataBind();
    }
}
